package com.wxd.modules.sys.controller;

import com.wxd.annotation.SysLog;
import com.wxd.modules.sys.entity.SysRoleEntity;
import com.wxd.modules.sys.service.SysRoleDeptService;
import com.wxd.modules.sys.service.SysRoleMenuService;
import com.wxd.modules.sys.service.SysRoleService;
import com.wxd.utils.PageUtils;
import com.wxd.utils.R;
import com.wxd.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *角色管理
 */

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;
    /**
     * 角色列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:role:list")
    public R list(@RequestParam Map<String,Object> params)
    {
        PageUtils page = sysRoleService.queryPage(params);
        return R.ok().put("page",page);
    }
    /**
     * 用户分配对应的角色列表
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:role:select")
    public R select()
    {
        PageUtils page = sysRoleService.queryPage(new HashMap<>());
        List<SysRoleEntity> list = (List<SysRoleEntity>) page.getList();
        return R.ok().put("list",list);
    }
    /**
     *角色信息
     */
    @RequestMapping("/info/{roleId}")
    @RequiresPermissions("sys:role:info")
     public R info(@PathVariable("roleId") Long roleId)
    {
        SysRoleEntity role = sysRoleService.getById(roleId);
        //查询角色对应菜单（功能）
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);
        //查询角色对应的部门
        List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(new Long[]{roleId});
        role.setDeptIdList(deptIdList);
        return R.ok().put("role",role);
    }
    /**
     * 保存角色
     */
    @SysLog("保存角色")
    @RequestMapping("/save")
    @RequiresPermissions("sys:role:save")
    public R save(@RequestBody SysRoleEntity role)
    {
        ValidatorUtils.validateEntity(role);//validator数据校验
        sysRoleService.saveRole(role);
        return  R.ok();
    }

    /**
     *  修改角色
     */
    @SysLog("修改角色")
    @RequestMapping("/update")
    @RequiresPermissions("sys:role:update")
    public R update(@RequestBody SysRoleEntity role)
    {
        ValidatorUtils.validateEntity(role);
        sysRoleService.update(role);
        return R.ok();
    }
    /**
     * 删除角色
     */
    @SysLog("删除角色")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public R delete(@RequestBody  Long[] roleIds)
    {
        sysRoleService.deleteBatch(roleIds);
        return R.ok();
    }
}
